#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
开放商户 - 进件查询相关模型
"""

from typing import Dict, Any, List, Optional
from datetime import datetime
from jlpay.model.base import OrgBaseReq, BaseResponse


class FieldDetail:
    """字段详情"""
    
    def __init__(self, field_name=None, field_value=None, error_msg=None):
        """
        初始化字段详情
        
        Args:
            field_name: 字段名称
            field_value: 字段值
            error_msg: 错误信息
        """
        self.field_name = field_name  # 字段名称
        self.field_value = field_value  # 字段值
        self.error_msg = error_msg  # 错误信息
    
    def to_dict(self) -> Dict[str, Any]:
        """
        转换为字典格式
        
        Returns:
            字典格式的数据
        """
        return {
            'field_name': self.field_name,
            'field_value': self.field_value,
            'error_msg': self.error_msg
        }


class AuditDetail:
    """审核详情"""
    
    def __init__(self, module=None, module_name=None, failed_reason=None, module_detail=None):
        """
        初始化审核详情
        
        Args:
            module: 模块变量名
            module_name: 模块中文名
            failed_reason: 模块审核不通过原因
            module_detail: 字段审核不通过原因列表
        """
        self.module = module  # 模块变量名
        self.module_name = module_name  # 模块中文名
        self.failed_reason = failed_reason  # 模块审核不通过原因
        self.module_detail = module_detail or []  # 字段审核不通过原因
    
    def to_dict(self) -> Dict[str, Any]:
        """
        转换为字典格式
        
        Returns:
            字典格式的数据
        """
        return {
            'module': self.module,
            'module_name': self.module_name,
            'failed_reason': self.failed_reason,
            'module_detail': [detail.to_dict() for detail in self.module_detail]
        }


class IncomingQueryRequest(OrgBaseReq):
    """进件查询请求"""
    
    API_URL = "/open/merch/access/incoming/query"
    
    def __init__(self):
        """初始化进件查询请求"""
        super().__init__()
        self.apply_id = None
    
    def set_apply_id(self, apply_id):
        """
        设置申请单号
        
        Args:
            apply_id: 申请单号
            
        Returns:
            IncomingQueryRequest实例
        """
        self.apply_id = apply_id
        return self
    
    def to_dict(self) -> Dict[str, Any]:
        """
        转换为字典格式
        
        Returns:
            字典格式的数据
        """
        data = super().to_dict()
        if self.apply_id:
            data['apply_id'] = self.apply_id
        return data


class IncomingQueryResponse(BaseResponse):
    """进件查询响应"""
    
    def __init__(self, response_dict=None):
        """
        初始化进件查询响应
        
        Args:
            response_dict: 响应字典
        """
        super().__init__(response_dict)
        self.apply_id = None  # 业务申请编号
        self.audit_status = None  # 审核状态 01：审核通过, 02：审核中， 09：审核驳回
        self.audit_detail = None  # 审核驳回详情
        self.audit_failure = None  # 审核驳回描述
        self.merch_no = None  # 商户号
        self.sign_id = None  # 协议编号
        self.sign_status = None  # 签约状态 00:未签约 01:已签约 02:签约中 09:签约失败
        self.sign_url = None  # 协议链接
        self.sign_delegatee_id = None  # 委托合同协议编号
        self.sign_delegatee_status = None  # 授权收款模式下，被授权人的签约状态
        self.sign_delegatee_url = None  # 授权收款模式下，被授权人的承诺函、电子协议签约链接
        self.term_no = None  # 终端号
        self.audit_end_time = None  # 审核时间
        self.unionpay_report_ret = None  # 银联渠道报备结果 01: 成功 02: 处理中 09: 失败
        self.unionpay_report_msg = None  # 银联报备结果描述
        self.wechat_report_ret = None  # 微信渠道报备结果 01: 成功 02: 处理中 09: 失败
        self.wechat_report_msg = None  # 微信报备结果描述
        self.alipay_report_ret = None  # 支付宝渠道报备结果 01: 成功 02: 处理中 09: 失败
        self.alipay_report_msg = None  # 支付宝报备结果描述
        self.sub_appid_bind_ret = None  # appid绑定结果 01: 成功 02: 处理中 09: 失败
        self.sub_appid_bind_msg = None  # appid绑定结果描述
        self.functions = None  # 业务开通情况
        
        if response_dict:
            self.apply_id = response_dict.get('apply_id')
            self.audit_status = response_dict.get('audit_status')
            self.audit_detail = response_dict.get('audit_detail')
            self.audit_failure = response_dict.get('audit_failure')
            self.merch_no = response_dict.get('merch_no')
            self.sign_id = response_dict.get('sign_id')
            self.sign_status = response_dict.get('sign_status')
            self.sign_url = response_dict.get('sign_url')
            self.sign_delegatee_id = response_dict.get('sign_delegatee_id')
            self.sign_delegatee_status = response_dict.get('sign_delegatee_status')
            self.sign_delegatee_url = response_dict.get('sign_delegatee_url')
            self.term_no = response_dict.get('term_no')
            self.audit_end_time = response_dict.get('audit_end_time')
            self.unionpay_report_ret = response_dict.get('unionpay_report_ret')
            self.unionpay_report_msg = response_dict.get('unionpay_report_msg')
            self.wechat_report_ret = response_dict.get('wechat_report_ret')
            self.wechat_report_msg = response_dict.get('wechat_report_msg')
            self.alipay_report_ret = response_dict.get('alipay_report_ret')
            self.alipay_report_msg = response_dict.get('alipay_report_msg')
            self.sub_appid_bind_ret = response_dict.get('sub_appid_bind_ret')
            self.sub_appid_bind_msg = response_dict.get('sub_appid_bind_msg')
            self.functions = response_dict.get('functions')
    
    def to_dict(self) -> Dict[str, Any]:
        """
        转换为字典格式
        
        Returns:
            字典格式的数据
        """
        data = super().to_dict()
        if self.apply_id:
            data['apply_id'] = self.apply_id
        if self.audit_status:
            data['audit_status'] = self.audit_status
        if self.audit_detail:
            data['audit_detail'] = self.audit_detail
        if self.audit_failure:
            data['audit_failure'] = self.audit_failure
        if self.merch_no:
            data['merch_no'] = self.merch_no
        if self.sign_id:
            data['sign_id'] = self.sign_id
        if self.sign_status:
            data['sign_status'] = self.sign_status
        if self.sign_url:
            data['sign_url'] = self.sign_url
        if self.sign_delegatee_id:
            data['sign_delegatee_id'] = self.sign_delegatee_id
        if self.sign_delegatee_status:
            data['sign_delegatee_status'] = self.sign_delegatee_status
        if self.sign_delegatee_url:
            data['sign_delegatee_url'] = self.sign_delegatee_url
        if self.term_no:
            data['term_no'] = self.term_no
        if self.audit_end_time:
            data['audit_end_time'] = self.audit_end_time
        if self.unionpay_report_ret:
            data['unionpay_report_ret'] = self.unionpay_report_ret
        if self.unionpay_report_msg:
            data['unionpay_report_msg'] = self.unionpay_report_msg
        if self.wechat_report_ret:
            data['wechat_report_ret'] = self.wechat_report_ret
        if self.wechat_report_msg:
            data['wechat_report_msg'] = self.wechat_report_msg
        if self.alipay_report_ret:
            data['alipay_report_ret'] = self.alipay_report_ret
        if self.alipay_report_msg:
            data['alipay_report_msg'] = self.alipay_report_msg
        if self.sub_appid_bind_ret:
            data['sub_appid_bind_ret'] = self.sub_appid_bind_ret
        if self.sub_appid_bind_msg:
            data['sub_appid_bind_msg'] = self.sub_appid_bind_msg
        if self.functions:
            data['functions'] = self.functions
        return data 